package com.lsx.logistics;

import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @Author: Liang
 * @Date: 2021/12/25
 * @Description:
 */
public class MD5Util {

    public static String getMD5(String content) {
        String result = "";
        try {
            MessageDigest md = MessageDigest.getInstance("md5");
            md.update(content.getBytes());
            byte[] bytes = md.digest();
            StringBuilder sb = new StringBuilder();
            for (byte b : bytes) {
                String str = Integer.toHexString(b & 0xFF);
                if (str.length() == 1) {
                    sb.append("0");
                }
                sb.append(str);
            }
            result = sb.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return result;
    }
    public static final String getMd5(String content, String salt){
        //加密方式
        String hashAlgorithmName = "MD5";
        //盐：为了即使相同的密码不同的盐加密后的结果也不同
        ByteSource byteSalt = ByteSource.Util.bytes(salt);
        //密码
        Object source = content;
        //加密次数
        int hashIterations = 2;
        SimpleHash result = new SimpleHash(hashAlgorithmName, source, byteSalt, hashIterations);
        return result.toString();
    }
    public static boolean isEqual(String content, String code){
        return code.equals(content);
    }
    public static void main(String[] args) {
        System.out.println(getMd5("123123", "NDT0001"));
        System.out.println(getMd5("123123", "NDT0002"));
        System.out.println(getMd5("123123", "NDT0003"));
        System.out.println(getMd5("123123", "NDT0004"));
        System.out.println(getMd5("123123", "NDT0005"));
        System.out.println(getMd5("123123", "NDT0006"));
        System.out.println(getMd5("123123", "NDT0007"));
        System.out.println(getMd5("123132", "NDT0008"));
        System.out.println(getMd5("123123", "NDT0009"));
        System.out.println(getMd5("123123", "NDT00010"));
        System.out.println(getMd5("123123", "NDT00011"));
        System.out.println(getMd5("123123", "NDT00012"));
    }
}
